package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.Dict;
import com.bwie.pojo.Prod;
import com.bwie.service.DictService;
import com.bwie.mapper.DictMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 陶仕豪
* @description 针对表【tb_dict】的数据库操作Service实现
* @createDate 2025-07-19 09:43:21
*/
@Service
@Slf4j
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict>
    implements DictService{

    @Autowired
    DictMapper dictMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate redisTemplate;

    @PostConstruct
    public void loadDictList(){
        log.info("加载字典列表开始-----");
        //list存储
//        List<Dict> list = dictMapper.selectList(null);
//        for (Dict dict : list) {
//            String key="dict:"+dict.getDictType();
//            stringRedisTemplate.opsForList().leftPush(key, JSON.toJSONString(dict));
//        }
        List<Dict> dictList = dictMapper.selectList(null);

        for (Dict dict : dictList) {
            String key1="dict:"+dict.getDictType();
            String key2=dict.getDictValue().toString();
            stringRedisTemplate.opsForHash().put(key1,key2,JSON.toJSONString(dict));
        }

        log.info("加载字典列表结束-----");
    }

    @Override
    public R getDictListById(Integer typeId) {
//        String key="dict:"+typeId;
//        List<String> stringList = stringRedisTemplate.opsForList().range(key, 0, -1);
//        if(stringList!=null&&stringList.size()>0){
//            List<Dict> dictList = stringList.stream().map(item -> JSON.parseObject(item, Dict.class)).collect(Collectors.toList());
//            return R.OK(dictList);
//        }
        String key1="dict:"+typeId;
        Boolean hasKey = stringRedisTemplate.hasKey(key1);
        if(hasKey){
            System.out.println("缓存中读取");
            List<Object> values = stringRedisTemplate.opsForHash().values(key1);
            List<Dict> dictList = values.stream().map((item -> {
                Dict dict = JSON.parseObject(item.toString(), Dict.class);
                return dict;
            })).collect(Collectors.toList());
            return R.OK(dictList);
        }
        //缓存中不存在 查询数据库
        List<Dict> dictList = dictMapper.selectList
                (new QueryWrapper<Dict>().lambda().eq(Dict::getDictType, typeId));
        for (Dict dict : dictList) {
            String key="dict:"+dict.getDictType();
            String key2=dict.getDictValue().toString();
            stringRedisTemplate.opsForHash().put(key,key2,JSON.toJSONString(dict));
        }
        return R.OK(dictList);
    }

    @Override
    public R getDictList(PageInfoVo pageInfoVo) {
        //构造分页参数
        Page<Dict> dictPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        //分页查询
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByAsc(Dict::getDictType).orderByAsc(Dict::getDictValue);
        Page<Dict> page = dictMapper.selectPage(dictPage, wrapper);
        return R.OK(page);
    }


}




